package com.hnucm.cooklife.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.bean.db.ClasSec;
import com.hnucm.cooklife.bean.db.OrderSec;
import com.hnucm.cooklife.bean.order.OrderData;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class OrderAdapter extends BaseSectionQuickAdapter<OrderSec, BaseViewHolder> {

    /**
     * 构造方法里， super()必须设置 header layout
     * data可有可无
     */
    private Context mContext;

    public void setContext(Context context) {
        this.mContext = context;
    }

//    //此变量类型为自定义接口类型
    private RecomAdapter.onItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(RecomAdapter.onItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public OrderAdapter(int layoutResId, int sectionHeadResId, List<OrderSec> data) {
        super(sectionHeadResId, data);
        // （只有这里有变化）
        // 添加 item 布局、类型
        addItemType(ClasSec.HEADER_TYPE, R.layout.clas_head_item);
        addItemType(ClasSec.NORMAL_TYPE, R.layout.order_item);
    }

    @Override
    protected void convertHeader(@NotNull BaseViewHolder baseViewHolder, @NotNull OrderSec orderSec) {
        if (orderSec.getObject() instanceof String) {
            baseViewHolder.setText(R.id.clas_head, (String) orderSec.getObject());
        }
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, OrderSec orderSec) {
        OrderData.DataDTO dto = (OrderData.DataDTO) orderSec.getObject();
        baseViewHolder.setText(R.id.order_name,dto.getOname());
        baseViewHolder.setText(R.id.order_cap,dto.getOcap());

        String price = String.format(getContext().getResources().getString(R.string.order_price), dto.getOprice());
        baseViewHolder.setText(R.id.order_price,price);

        String sale = String.format(getContext().getResources().getString(R.string.order_sale), dto.getOsold());
        baseViewHolder.setText(R.id.order_sale,sale);

        baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int itemPosition = getItemPosition(orderSec);
                if(itemPosition != RecyclerView.NO_POSITION){
                    mOnItemClickListener.onSeleClickListener(baseViewHolder.itemView,itemPosition);
                }
            }
        });
        Glide.with(mContext)
                .load(dto.getOrderpicList())
                .into((ImageView) baseViewHolder.getView(R.id.order_pic));
    }

    @Override
    public void onAttachedToRecyclerView(@NotNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager){
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    return (type == ClasSec.HEADER_TYPE) ? ((GridLayoutManager) manager).getSpanCount() : 1;
                }
            });
        }
    }
}
